<template>
<!-- 评论头部 -->
  <div class="goods-comment" v-if="commentinfo">
    <div class="head">
      <div class="data">
        <p><span>{{commentinfo.salesCount}}</span><span>人购买</span></p>
        <p><span>{{commentinfo.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a href="javascript:;" :class="{active:actives===index}" v-for="(item,index) in commentinfo.tags" :key="index"
          @click="change(item,index)" >{{item.title}}（{{item.tagCount}}）</a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" :class="{active:tet===text}" v-for="tet in sortlist" :key="tet.name" @click="sortchange(tet)">{{tet.name}}</a>
    </div>
    <div class="list"></div>
  </div>
  <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="item in commentattr" :key="item.id">
        <div class="user">
          <img v-lazy="item.member.avatar" alt="">
          <span>{{filtername(item.member.nickname)}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="b in item.score" :key="b"></i>
            <i class="iconfont icon-wjx02" v-for="a in 5-item.score" :key="a"></i>
            <span class="attr" v-for="v in item.orderInfo.specs" :key="v.name">{{v.name}}:{{v.nameValue}}</span>
          </div>
          <div class="text">{{item.content}}
            <GoodsCommentImage :pictures='item.pictures'></GoodsCommentImage>
          </div>
          <div class="time">
            <span>{{item.orderInfo.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>

        </div>
      </div>
       <XtxPagination :total='total' @pagechange='pagechange' :count='query.page' :pageSize='query.pageSize'></XtxPagination>
    </div>
</template>
<script>
import { findCommentInfo, findGoodsCommentList } from '@/api/goods'
import GoodsCommentImage from '../goods-comment-image.vue'
import { inject, reactive, ref, watch } from 'vue'
export default {
  components: {
    GoodsCommentImage
  },
  name: 'GoodsComment',
  setup () {
    const text = ref(null)
    const actives = ref(0)
    const commentinfo = ref(null)
    const goods = inject('goods')
    //  获取头部数据

    findCommentInfo(goods.value.id).then(res => {
      goods.value.max = res.result.evaluateCount
      commentinfo.value = res.result
      commentinfo.value.tags.unshift({ title: '全部评论', tagCount: commentinfo.value.evaluateCount },
        { title: '有图', tagCount: commentinfo.value.hasPictureCount })
    })
    // 获取评论列表数据
    // 1.准备筛选条件
    const page = ref(null)
    const query = reactive({
      page: page.value | 1, // 页码
      pageSize: 10, // 每页数量
      hasPicture: null, // 显示有图
      tag: null, // 显示标签
      sortField: null// 排序
    })
    const total = ref(null)

    const commentattr = ref([])
    watch(query, () => {
      findGoodsCommentList(goods.value.id, query).then(res => {
        commentattr.value = res.result.items
        total.value = res.result.counts
        page.value = res.result.page
        console.log(res.result)
      })
    }, { immediate: true })
    // 过滤名称
    const filtername = (name) => {
      return name.slice(0, 1) + '****' + name.slice(-1)
    }
    // 根据条件筛选显示数据
    const change = (itme, index) => {
      actives.value = index
      console.log(itme)
      if (itme.title === '有图') {
        query.hasPicture = true
        query.page = 1
      } else if (itme.title === '全部评论') {
        query.hasPicture = null
        query.tag = null
        query.page = 1
        query.sortField = null
      } else {
        query.page = 1
        query.tag = itme.title
      }
    }
    // 根据排序显示数据
    const sortlist = [{
      name: '默认',
      value: null
    },
    {
      name: '最新',
      value: 'createTime'
    },
    {
      name: '最热',
      value: 'praiseCount'
    }]
    // 修改排序显示数据
    const sortchange = (tet) => {
      query.page = 1
      text.value = tet.name
      query.sortField = tet.value
    }
    // 根据分页器更新数据
    const pagechange = (count) => {
      query.page = count
    }
    return { commentinfo, actives, commentattr, filtername, change, sortlist, sortchange, text, pagechange, query, total }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor,50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,&:hover {
        color: @xtxColor;
      }
    }
  }
}
// 评论列表
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
</style>
